package com.atguigu.cloud.common.mail.controller;

import com.atguigu.cloud.common.https.entity.Result;
import com.atguigu.cloud.common.https.enums.CodeMsgEnum;
import com.atguigu.cloud.common.mail.service.MailService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 邮件发送控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/mail")
@RequiredArgsConstructor
public class MailController {
    
    private final MailService mailService;

    /**
     * 发送生日邮件
     * @param json 邮件内容JSON
     * @return 发送结果
     */
    @PostMapping("/birth")
    public Result<String> sendBirthMail(@RequestBody String json) {
        if (!StringUtils.hasText(json)) {
            return Result.error(CodeMsgEnum.PARAM_ERROR, "邮件内容不能为空");
        }

        try {
            mailService.sendEmail(json);
            log.info("生日邮件发送成功");
            return Result.success("邮件发送成功");
        } catch (Exception e) {
            log.error("生日邮件发送失败：{}", e.getMessage(), e);
            return Result.error(CodeMsgEnum.SERVICE_ERROR, "邮件发送失败：" + e.getMessage());
        }
    }
}
